package com.dotflat.pay.controller;

import com.google.common.collect.ImmutableMap;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.vo.BindedBabyVO;
import com.starinside.wp.utils.RemoteCallUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Slf4j
@RestController
@RequestMapping("/v2/pay/walletflow")
@Api(tags = "缴费 - 钱包流水")
public class WalletFlowController {

    @Value("${server.crm.url}")
    private String crmServerURL;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object getFlowList(HttpServletRequest req,
                              @RequestHeader(value = TICKET, required = false) String ticket,
                              @RequestParam Integer type,
                              @RequestParam Integer sourceType,
                              @RequestParam Integer pageNum,
                              @RequestParam Integer pageSize ){


        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");

        BindedBabyVO baby = user.getDefaultBaby();
        if (baby == null){
            throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "宝宝不存在");
        }

//        areaId, String clientId
        Map p = ImmutableMap.of("areaId", baby.getAreaId(), "clientId", baby.getCrmClienteleId());

//        @ApiModelProperty( "INT COMMENT '类型：0.无变化 1. 进账 2. 出账'")
//        private Integer type;
//        @ApiModelProperty( "INT COMMENT '来源类型：0. 无变化 1. 现金动账 2. 项目操作 3. 系统操作'")
//        private Integer sourceType;
//        private Integer pageNum = 0;
//        private Integer pageSize = 10;
        if (type != null){
            p.put("type", type);
        }
        if (type != null){
            p.put("sourceType", sourceType);
        }
        if (type != null){
            p.put("pageNum", pageNum);
        }
        if (type != null){
            p.put("pageSize", pageSize);
        }

        HashMap result = RemoteCallUtil.call(crmServerURL + "/api/other_system/walletFlow/list", p, null, HashMap.class, "获取流水失败");

        return StatusResponse.success(result);
    }
}
